package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;
import com.xbongbong.pro.form.pojo.RuleTargetPojo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName ConditionFormStageHandlerImpl
 * @Description 阶段推进器字段解析
 * @Author sunshaoxiang
 * @Date 2021/12/26 16:06
 * @Version 1.0
 **/
@Component
public class ConditionFormStageHandlerImpl extends AbstractConditionFieldHandler {

    @Resource
    private ProFormHelp proFormHelp;

    @Resource
    private Map<Integer, FieldAnalyseService> fieldAnalyseMap;

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.STAGE_THRUSTER.getType());
    }
    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException {
        Object object;
        String corpid = businessRuleInfoPojo.getCorpid();
        Integer saasMark = businessRuleInfoPojo.getSaasMark();
        Integer businessType = businessRuleInfoPojo.getBusinessType();
        Long formId = businessRuleInfoPojo.getFormId();
        // 根据value获取text
        List<ItemPoJo> itemPoJos = proFormHelp.getEditStageComboItems(fieldAttrEntity, saasMark, businessType, corpid, formId, data);
        fieldAttrEntity.setItems(itemPoJos);
        object = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        return object;
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        String corpid = businessRuleInfoPojo.getCorpid();
        RuleTargetPojo ruleTargetPojo = businessRuleInfoPojo.getRuleTargetPojo();
        Object object = businessRuleInfoPojo.getObject();
        Long linkFormId = ruleTargetPojo.getFormId();
        Integer linkSaasMark = ruleTargetPojo.getLinkSaasMark();
        Integer linkBusinessType = ruleTargetPojo.getLinkBusinessType();
        List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(linkSaasMark, linkBusinessType, corpid, linkFormId);
        targetField.setItems(itemPoJos);
        return ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        Object object = filterCondition.getValue();
        String corpid = businessRuleInfoPojo.getCorpid();
        RuleTargetPojo ruleTargetPojo = businessRuleInfoPojo.getRuleTargetPojo();
        Long linkFormId = ruleTargetPojo.getFormId();
        Integer linkSaasMark = ruleTargetPojo.getLinkSaasMark();
        Integer linkBusinessType = ruleTargetPojo.getLinkBusinessType();
        List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(linkSaasMark, linkBusinessType, corpid, linkFormId);
        targetField.setItems(itemPoJos);
        object = ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
        return object;
    }

    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity targetField, FieldAttrEntity relyFieldEntity, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (value instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) value;
            value = map.getOrDefault("text", "");
        }
        Integer linkSaasMark = linkInfoPojo.getLinkSaasMark();
        Integer linkBusinessType = linkInfoPojo.getLinkBusinessType();
        Long linkFormId = linkInfoPojo.getLinkFormId();
        List<ItemPoJo> itemList = proFormHelp.getEnableStageComboItems(linkSaasMark, linkBusinessType, corpid, linkFormId);
        Map<Object, Object> itemMap = new HashMap<>(itemList.size());
        itemList.forEach(item -> itemMap.put(item.getText(), item.getValue()));
        return itemMap.get(value);
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity targetField, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        Integer linkSaasMark = linkInfoPojo.getLinkSaasMark();
        Integer linkBusinessType = linkInfoPojo.getLinkBusinessType();
        Long linkFormId = linkInfoPojo.getLinkFormId();
        List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(linkSaasMark, linkBusinessType, corpid, linkFormId);
        targetField.setItems(itemPoJos);
        value = ItemUtil.getCustomItemValue(targetField, String.valueOf(value));
        return value;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity relyFieldEntity, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        // 从valueMap里获取text，对比目标表单的text，拿出目标表单的value
        List<ItemPoJo> itemPoJoList = proFormHelp.getEnableStageComboItems(linkSaasMark, linkBusinessType, corpid, linkFormId);
        Map<Object, ItemPoJo> itemMap = new HashMap<>(itemPoJoList.size());
        for (ItemPoJo itemPoJo : itemPoJoList) {
            itemMap.put(itemPoJo.getText(), itemPoJo);
        }
        if (Objects.nonNull(itemMap.get(value))) {
            value = itemMap.get(value).getValue();
        }
        return value;
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity targetField, ConditionDTO conditionDTO, Object value) throws XbbException {
        List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, conditionDTO.getLinkBusinessType(), corpid, conditionDTO.getLinkFormId());
        targetField.setItems(itemPoJos);
        value = ItemUtil.getCustomItemValue(targetField, String.valueOf(value));
        return value;
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        FieldAnalyseService fieldAnalyseService = fieldAnalyseMap.get(thisField.getFieldType());
        return fieldAnalyseService.handlerPreConditionValue(thisField, businessRuleInfoPojo, filterCondition);
    }
}
